﻿using System;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using Common.Logging;
using In.Api.Dto.HisWs;
using Newtonsoft.Json;
using OutDbContext;
using User.Api.Helpers;
using User.Api.HttpClients;
using User.Api.Dto.Depression;

namespace User.Api.Controllers
{
    public class PostpartumDepressionController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(PostpartumDepressionController));
        private FsssdqfybjyEntities _ctx;
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            _ctx = new FsssdqfybjyEntities();
            base.Initialize(controllerContext);
        }

        protected override void Dispose(bool disposing)
        {
            _ctx.Dispose();
            base.Dispose(disposing);
        }


        /// <summary>
        /// 获取产妇信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetMaternalInfo")]
        public GetMaternalInfoResponse GetMaternalInfo(string cardNo, string name)
        {
            //获取产妇信息
            var request4GetMaternal = new Request4GetMaternalInfo();
            request4GetMaternal.WardNo = cardNo;

            Response4GetMaternalInfo response4GetMaternal;
            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetMaternalInfo", request4GetMaternal).Result;
                responseMsg.EnsureSuccessStatusCode();
                response4GetMaternal = responseMsg.Content.ReadAsAsync<Response4GetMaternalInfo>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("获取GetMaternalInfo Api出错,信息：{0}", JsonConvert.SerializeObject(ex));
                return new GetMaternalInfoResponse
                {
                    ResultCode = "3",
                    ResultMsg = "服务器或网络异常，请联系客服"
                };
            }

            if (response4GetMaternal.ResultCode != "0")
            {
                return new GetMaternalInfoResponse
                {
                    ResultCode = "4",
                    ResultMsg = response4GetMaternal.ResultDesc
                };
            }
            if (response4GetMaternal.MaternalInfo.Name.Trim() != name.Trim())
            {
                return new GetMaternalInfoResponse
                {
                    ResultCode = "2",
                    ResultMsg = "操作失败，住院号与姓名不匹配"
                };
            }
            var responseMaternalInfo = response4GetMaternal.MaternalInfo;
            var MaternalInfo = new GetMaternalInfoResponse.MaternalList()
            {
                Name = responseMaternalInfo.Name,
                Age = responseMaternalInfo.Age,
                IDCard = responseMaternalInfo.IDCard,
                CardNo = responseMaternalInfo.CardNo,
                PregnancyDT = responseMaternalInfo.PregnancyDT,
                ChildbirthDT = responseMaternalInfo.ChildbirthDT
            };

            return new GetMaternalInfoResponse()
            {
                ResultCode = "0",
                ResultMsg = "",
                ClinPatientInfo = MaternalInfo
            };
        }

        /// <summary>
        /// 提交满意度调查
        /// </summary>
        /// <param name="paperId">问卷Id</param>
        /// <param name="socre1">得分1</param>
        /// <param name="socre2">得分2</param>
        /// <param name="socre3">得分3</param>
        /// <param name="socre4">得分4</param>
        /// <param name="socre5">得分5</param>
        /// <param name="socre6">得分6</param>
        /// <param name="socre7">得分7</param>
        /// <param name="socre8">得分8</param>
        /// <param name="socre9">得分9</param>
        /// <param name="socre10">得分10</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("SubmitTiJiao")]
        public SubmitTiJiaoResponse SubmitTiJiao(string patientId, string name, string age, string IDCard, string pregnancyDT, string childbirthDT, int socre1, int socre2, int socre3, int socre4, int socre5, int socre6,
            int socre7, int socre8, int socre9, int socre10)
        {
            var azUser = AzUser.GetInApi(Request);
            var openId = azUser.OpenId;

            try
            {
                var post = new PostpartumDepression
                {
                    PatientId = patientId,
                    socre1 = socre1,
                    socre2 = socre2,
                    socre3 = socre3,
                    socre4 = socre4,
                    socre5 = socre5,
                    socre6 = socre6,
                    socre7 = socre7,
                    socre8 = socre8,
                    socre9 = socre9,
                    socre10 = socre10,
                    Name = name,
                    Age = age,
                    IDCard = IDCard,
                    PregnancyDT = patientId,
                    ChildbirthDT = childbirthDT,
                    Status = 1,
                    SubmitTime = DateTime.Now,
                    CardNo = patientId,
                    Sum = socre1 + socre2 + socre3 + socre4 + socre5 + socre6 + socre7 + socre8 + socre9 + socre10
                };
                _ctx.PostpartumDepressions.Add(post);
                _ctx.SaveChanges();

            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("提交满意度调查 出错，原因：{0}", ex.Message);

                return new SubmitTiJiaoResponse()
                {
                    ResultCode = "1",
                    ResultMsg = ""
                };
            }

            return new SubmitTiJiaoResponse()
            {
                ResultCode = "0",
                ResultMsg = ""
            };
        }
    }
}